package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelPicShowAdapter;
import flc.ast.adapter.SelPictureAdapter;
import flc.ast.bean.SelPicShowBean;
import flc.ast.databinding.ActivitySelPicBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelPicActivity extends BaseAc<ActivitySelPicBinding> {

    public static boolean isMore = false;
    private SelPictureAdapter pictureAdapter;
    private List<SelectMediaEntity> listShow = new ArrayList<>();
    private List<SelPicShowBean> listBottom = new ArrayList<>();
    private SelPicShowAdapter showAdapter;
    private String selPath = "";
    private int oldPosition = 0;

    @Override
    protected int onCreate() {
        return R.layout.activity_sel_pic;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelPicBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.llSelPicBottom.setOnClickListener(null);
        mDataBinding.tvSelPicComp.setOnClickListener(this);
        mDataBinding.rvPicList.setLayoutManager(new GridLayoutManager(mContext, 3));
        pictureAdapter = new SelPictureAdapter();
        pictureAdapter.setMore(isMore);
        mDataBinding.rvPicList.setAdapter(pictureAdapter);
        pictureAdapter.setOnItemClickListener(this);
        //底部选中图片
        showAdapter = new SelPicShowAdapter();
        mDataBinding.rvSelPictureShowList.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.HORIZONTAL,false));
        mDataBinding.rvSelPictureShowList.setAdapter(showAdapter);
        showAdapter.addChildClickViewIds(R.id.ivSelPicShowItemCha);
        showAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()){
            case R.id.tvSelPicComp:
                List<String> listPath=new ArrayList<>();
                for (SelPicShowBean selPicShowBean : listBottom) {
                    if (!TextUtils.isEmpty(selPicShowBean.getPath())){
                        listPath.add(selPicShowBean.getPath());
                    }
                }
                if (listPath.size()==0){
                    ToastUtils.showShort(getString(R.string.please_sel_picture));
                    return;
                }
                PuzzleActivity.listPath=listPath;
                startActivity(PuzzleActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (isMore) {
            if (pictureAdapter.getItem(position).isChecked()) {
                pictureAdapter.getItem(position).setChecked(false);
                pictureAdapter.notifyItemChanged(position);
                deleteShow(pictureAdapter.getItem(position).getPath());
            } else {
                pictureAdapter.getItem(position).setChecked(true);
                addShow(pictureAdapter.getItem(position).getPath(), position);
            }
        } else {
            selPath = pictureAdapter.getItem(position).getPath();
            PreviewActivity.isCamera = false;
            PreviewActivity.resultPath = selPath;
            startActivity(PreviewActivity.class);
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()){
            case R.id.ivSelPicShowItemCha:
                if (!TextUtils.isEmpty(showAdapter.getItem(position).getPath())){
                    String path=showAdapter.getItem(position).getPath();
                    for (int i = 0; i < pictureAdapter.getValidData().size(); i++) {
                        if (path.equals(pictureAdapter.getItem(i).getPath())){
                            pictureAdapter.getItem(i).setChecked(false);
                            pictureAdapter.notifyItemChanged(i);
                        }
                    }
                    deleteShow(path);
                }
                break;
        }
    }

    @Override
    protected void initData() {
        getPermission();
        if (isMore){
            mDataBinding.llSelPicBottom.setVisibility(View.VISIBLE);
        }else {
            mDataBinding.llSelPicBottom.setVisibility(View.GONE);
        }
        //准备底部数据
        listBottom.add(new SelPicShowBean("", 1, false));
        listBottom.add(new SelPicShowBean("", 2, false));
        listBottom.add(new SelPicShowBean("", 3, false));
        listBottom.add(new SelPicShowBean("", 4, false));
        listBottom.add(new SelPicShowBean("", 5, false));
        listBottom.add(new SelPicShowBean("", 6, false));
        listBottom.add(new SelPicShowBean("", 7, false));
        listBottom.add(new SelPicShowBean("", 8, false));
        listBottom.add(new SelPicShowBean("", 9, false));
        mDataBinding.tvSelPicComp.setText(getString(R.string.sel_pic_comp,0));
        showAdapter.setList(listBottom);
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_picture_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.rvPicList.setVisibility(View.GONE);
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        listShow.clear();
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.PHOTO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    listShow.addAll(selectMediaEntities);
                    pictureAdapter.setList(listShow);
                    mDataBinding.rvPicList.setVisibility(View.VISIBLE);
                    mDataBinding.tvSelPicNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvPicList.setVisibility(View.GONE);
                    mDataBinding.tvSelPicNoData.setVisibility(View.VISIBLE);
                }
            }
        });

    }

    //添加展示
    private void addShow(String path, int position) {
        //路径不为空的数量
        int num = 0;
        for (SelPicShowBean selPicShowBean : listBottom) {
            if (!TextUtils.isEmpty(selPicShowBean.getPath())) {
                num++;
            }
        }
        if (num >= 9) {
            ToastUtils.showShort(getString(R.string.sel_pic_tip));
            return;
        }
        pictureAdapter.getItem(position).setChecked(true);
        pictureAdapter.notifyItemChanged(position);
        listBottom.get(num).setPath(path);
        listBottom.get(num).setSelect(false);
        if (num < 8) {
            listBottom.get(num + 1).setSelect(true);
        }
        showAdapter.setList(listBottom);
        mDataBinding.tvSelPicComp.setText(getString(R.string.sel_pic_comp, num + 1));
    }

    //删除展示
    private void deleteShow(String path) {
        List<String> list = new ArrayList<>();
        for (SelPicShowBean selPicShowBean : listBottom) {
            if (!TextUtils.isEmpty(selPicShowBean.getPath())) {
                list.add(selPicShowBean.getPath());
            }
        }
        //删除选择的图片路径
        list.remove(path);
        for (SelPicShowBean selPicShowBean : listBottom) {
            selPicShowBean.setPath("");
            selPicShowBean.setSelect(false);
        }
        for (int i = 0; i < list.size(); i++) {
            listBottom.get(i).setPath(list.get(i));
        }
        listBottom.get(list.size()).setSelect(true);
        showAdapter.setList(listBottom);
        mDataBinding.tvSelPicComp.setText(getString(R.string.sel_pic_comp, list.size()));

    }

}